package com.sample.systemui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.sample.systemui.common.Constant;
import com.sample.systemui.view.NavigationBar;
import com.sample.systemui.view.Panel;
import com.sample.systemui.view.StatusBarServiceFragment;
import com.sample.servicefragment.CustomLifecycleService;

public class SystemUIService extends CustomLifecycleService {

    private static final String TAG = "===SystemUIService";
    private StatusBarServiceFragment mStatusBarFragment = null;
    private NavigationBar mNavigationBar = null;
    private Panel mPanel = null;

    private BroadcastReceiver mLocalReceiver = null;
    private IntentFilter mLocalFilter = null;

    public SystemUIService() {
        super();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    private Handler mHandler = null;

    @Override
    public void onCreate() {
        super.onCreate();
        initLocalBroadcast();

        mHandler = new Handler(getMainLooper());
        mHandler.post(new Runnable() {
            @Override
            public void run() {
//                showStatusBar();
                /**
                 * Sample6:step5
                 * 调用内部方法，创建 Panel对象，并显示。
                 */
//                showPanel();
                /**
                 * Sample6:step6
                 * 将Panel基类中的 OnTouchListener 绑定到 statusBar。
                 * 非必须步骤，加上此步骤是为了实现在statusBar上拖动时，能把 Panel 拉出来或者推进去。
                 * 要实现此效果，需要 StatusBarServiceFragment 中作一点改动：
                 * 请参考：Sample6:step6-1 与 Sample6:step6-2
                 */
//                mStatusBarFragment.setOnTouchListener(mPanel.getOnTouchListener());

                showNavigationBar();
            }
        });
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(getClass().getCanonicalName(), "onStartCommand.");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }


    private void showStatusBar() {

        Log.d(this.getClass().getCanonicalName(), "service started, to show Fragment.");

        mStatusBarFragment = new StatusBarServiceFragment();
        if(null != mPanel) {
            View.OnTouchListener listener = mPanel.getOnTouchListener();
            mStatusBarFragment.setOnTouchListener(listener);
        }
        getSupportFragmentManager().beginTransaction().add(mStatusBarFragment, "StatusBar").commit();
    }

    private void hideStatusBar() {
        getSupportFragmentManager().beginTransaction().remove(mStatusBarFragment).commit();
    }

    /**
     * Sample6:step4
     * 内部方法，创建 Panel对象，并显示。
     */
    private void showPanel() {
        Log.d(TAG, "service started, to create panelview.");
        mPanel = new Panel();
        getSupportFragmentManager().beginTransaction().add(mPanel, "Panel").commit();
    }


    private void showNavigationBar() {
        Log.d(this.getClass().getCanonicalName(), "service started, to show Fragment.");

        mNavigationBar = new NavigationBar();

        getSupportFragmentManager().beginTransaction().add(mNavigationBar, "StatusBar").commit();
    }

    private void hideNavigationBar() {
        getSupportFragmentManager().beginTransaction().remove(mNavigationBar).commit();
    }


    private void initLocalBroadcast() {
        mLocalReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                parseIntent(intent);
            }
        };

        mLocalFilter = new IntentFilter();
        mLocalFilter.addAction(Constant.LOCAL_ACTION_XXX);

        LocalBroadcastManager.getInstance(SystemUIApp.getMyApplicationContext()).registerReceiver(
                mLocalReceiver, mLocalFilter);
    }

    private void parseIntent(Intent intent) {
        String action = null;
        if((null == intent) || TextUtils.isEmpty(action = intent.getAction())) {
            return;
        }
        switch (action) {
            case Constant.LOCAL_ACTION_XXX:
                onActionXxx();
                break;
            // todo 添加其它需要处理的action 分支
            default:
                break;
        }
    }

    private void onActionXxx() {
        // todo something,such as showing a pop view.
    }

}
